package com.damie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.damie.domain.WasteOrder;
import com.damie.domain.WasteOrderDetails;
import com.damie.dto.OrderPageDto;
import com.damie.param.OrderPageParam;
import com.damie.service.WasteOrderService;
import com.damie.utils.Result;
import com.damie.utils.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Alon
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/damie/waste-order")
@Api(description = "用户订单模块服务")
@Slf4j
public class WasteOrderController {
    @Autowired
    private WasteOrderService wasteOrderService;

    /**
     * 订单分页查询
     * @return
     */
    @GetMapping("/pageOrder")
    @ApiOperation(value = "订单详情功能")
    public Result pageOrder(OrderPageDto pageDto, HttpServletRequest request){
        log.info("{}",pageDto);
        if (ObjectUtils.isEmpty(pageDto)){
            return new Result(false, StatusCode.NULL_ERROR,"参数不能为空",null);
        }
        OrderPageParam pages = new OrderPageParam();
        pages.setPage(new Page(pageDto.getCurrent(),pageDto.getSize()));
        pages.setUserId("1351497207160848386");//用户id
        pages.setStatus(pageDto.getStatus());//用户状态
        List<WasteOrder> wasteOrders = wasteOrderService.pageInfo(pages,request);
        if (ObjectUtils.isEmpty(wasteOrders)){
            return new Result(false,StatusCode.NULL_ERROR,"您还未有任何下单操作",null);
        }
        return Result.success(wasteOrders);
    }

    @GetMapping("/detailsInfo/{wasteOrderId}")
    @ApiOperation(value = "订单详情表")
    public Result detailsInfo(@PathVariable("wasteOrderId") String wasteOrderId){
        log.info("{}",wasteOrderId);
        if (StringUtils.isEmpty(wasteOrderId)){
            return new Result(false, StatusCode.NULL_ERROR,"参数不能为空",null);
        }
        List<WasteOrderDetails> wasteOrderDetails = wasteOrderService.detailsInfo(wasteOrderId);
        if (ObjectUtils.isEmpty(wasteOrderDetails)){
            return new Result(false,StatusCode.NULL_ERROR,"您还未有任何下单操作",null);
        }
        return Result.success(wasteOrderDetails);
    }

    @GetMapping("/press")
    @ApiOperation(value = "催单功能")
    public Result press(String oid){
        if (StringUtils.isEmpty(oid)){
            return new Result(false,StatusCode.NULL_ERROR,"订单号不能为空");
        }
        Boolean press = wasteOrderService.press(oid);
        if (press){
            return Result.success("催单成功");
        }
        return Result.fail("催单失败");
    }

}

